#!/bin/bash
# This script builds the gadgets.
# Syntax:
#   build.sh [-h <gadget hosting server>] [-s <backend server>] [-t] [-N]
#
#   Options:
#     -h - The URL of the server that will host the gadgets' .xml and .js files
#     -s - The URL of the backend server. Must be of this form: https://<ip|FQDN>/
#     -t - If given, the output is not compressed, just concatenated. Also, hosting server
#          and python backend server URLs are not written into the code.
#     -N - No cleaning of the build folder before building
#
#  History:
#    Before July 2013 - Created by Chris/Alain
#    13/07/2013  vuh - Added the -h, -s and -t options
#    19/07/2013  vuh - MN00488970 - replace SOCKET_SERVER with  BACKEND_SERVER

HOST="{{ HOSTNAME }}"
BACKENDSERVER="{{ BACKENDSERVER }}"
COMPILE_BOOL="compiled"
CLEAN_BUILD="Y"

while getopts "h:s:tN" options; do
  case $options in
    h) HOST=$OPTARG;;
    s) BACKENDSERVER=$OPTARG;;
    t) COMPILE_BOOL="script";;
    N) CLEAN_BUILD="N";;
    *) echo -e "Usage: $0 [-h <gadget hosting server URL>] [-s <backend server URL>] [-t] [-N]"
         echo -e "-h The URL of the server that will host gadget xml and js files. E.g. https://file.server.com/files"
         echo -e "-s The URL of the backend server. E.g. https://10.35.29.220/ or https://backend.server.com/a/"
         echo -e "-t Use it to NOT compile/compress the gadget code"
         echo -e "-N Don't clean the build folder before building"
         exit 1;;
  esac
done

# Ensure a single trailing slash for URLs
HOST=${HOST%/}"/"
BACKENDSERVER=${BACKENDSERVER%/}"/"

[ $CLEAN_BUILD = "Y" ] && rm -fr build

unset SSH_ASKPASS

mkdir -p build/
mkdir -p build/js
mkdir -p build/css
mkdir -p build/images

# Replace place-holder with first real data
function replace {
  FILE=`cat $1`
  echo "${FILE//$2/$3}"
}

java -jar SoyToJsSrcCompiler.jar  --outputPathFormat src/js/widget.js --shouldProvideRequireSoyNamespaces src/templates/*.soy

# Compile JS using Google Closure Compiler 

#java -jar compiler.jar --js src/js/socket.io.js | gzip -9 > build/js/socket.io.js


#   --output_mode=compiled \

python2.7 closure-library/closure/bin/build/closurebuilder.py \
   --root=closure-library \
   --root=src/ \
   --namespace="mitel.contextual_gadget" \
   --compiler_flags="--externs=src/js/externs/google.js" \
   --compiler_flags="--define=goog.DEBUG=true" \
   --output_mode=$COMPILE_BOOL \
   --compiler_flags="--define=GADGET_HOST='$HOST'" \
   --compiler_flags="--define=BACKEND_SERVER='$BACKENDSERVER'" \
   --compiler_jar=compiler.jar > build/js/gadget.js
   
python2.7 closure-library/closure/bin/build/closurebuilder.py \
   --root=closure-library \
   --root=src/ \
   --namespace="mitel.sidebar_gadget" \
   --compiler_flags="--externs=src/js/externs/google.js" \
   --compiler_flags="--define=goog.DEBUG=true" \
   --output_mode=$COMPILE_BOOL \
   --compiler_flags="--define=GADGET_HOST='$HOST'" \
   --compiler_flags="--define=BACKEND_SERVER='$BACKENDSERVER'" \
   --compiler_jar=compiler.jar > build/js/sidebar_gadget.js

python2.7 closure-library/closure/bin/build/closurebuilder.py \
   --root=closure-library \
   --root=src/ \
   --namespace="mitel.calendar_gadget" \
   --compiler_flags="--externs=src/js/externs/google.js" \
   --compiler_flags="--define=goog.DEBUG=true" \
   --output_mode=$COMPILE_BOOL \
   --compiler_flags="--define=GADGET_HOST='$HOST'" \
   --compiler_flags="--define=BACKEND_SERVER='$BACKENDSERVER'" \
   --compiler_jar=compiler.jar > build/js/calendar_gadget.js


# Compile the stylesheet with the closure-stylesheets.jar processor
java -jar closure-stylesheets.jar src/css/style.css > build/css/style.css
java -jar closure-stylesheets.jar src/css/calendar.css > build/css/calendar.css

replace src/gadget.xml "{{ HOSTNAME }}" $HOST > build/gadget.xml
replace src/sidebargadget.xml "{{ HOSTNAME }}" $HOST  > build/sidebar_gadget.xml
replace src/calendar_gadget.xml "{{ HOSTNAME }}" $HOST  > build/calendar_gadget.xml

# If you used the -t option, GADGET_HOST and BACKEND_SERVER have not been set
# We now set it for you at no extra cost:
sed "s@<<HOST>>@${HOST}@g" build/js/gadget.js > /tmp/gadget.js.1
sed "s@<<BACKENDSERVER>>@${BACKENDSERVER}@g" /tmp/gadget.js.1 > build/js/gadget.js
sed "s@<<HOST>>@${HOST}@g" build/js/sidebar_gadget.js > /tmp/sidebar_gadget.js.1
sed "s@<<BACKENDSERVER>>@${BACKENDSERVER}@g" /tmp/sidebar_gadget.js.1 > build/js/sidebar_gadget.js
sed "s@<<HOST>>@${HOST}@g" build/js/calendar_gadget.js > /tmp/calendar_gadget.js.1
sed "s@<<BACKENDSERVER>>@${BACKENDSERVER}@g" /tmp/calendar_gadget.js.1 > build/js/calendar_gadget.js

 
cp -Rp src/images/* build/images/

